//
//  V4SearchViewController.m
//  XinKaiFa55like
//
//  Created by 55like on 25/09/2017.
//  Copyright © 2017 55like lj. All rights reserved.
//

#import "V4SearchViewController.h"
#import "V4LicensePlateViewController.h"
#import "MyUiIAlertView.h"

@interface V4SearchViewController ()<UITextFieldDelegate,UIScrollViewDelegate>
{
    
    UIScrollView*scrollView;
    UIView*_historyView;
    UIView*_discoverView;
    
}
@property(nonatomic,strong)NSDictionary*dateDic;
@property(nonatomic,weak)UIView*discoverBtnView;
@property(nonatomic,weak)UITextField*searchTf;
@property(nonatomic,strong)UILabel*showmesglb;



@property(nonatomic,copy)NSString*successStr;

@end

@implementation V4SearchViewController
#pragma mark  开始
- (void)viewDidLoad {
    [super viewDidLoad];
    [self addSearchView];
//    [self addAndRefreshView];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self loadDATA];
    [_searchTf becomeFirstResponder];

}
#pragma mark -  写UI

-(void)addSearchView{
    
    UIView*viewSeach=[RHMethods viewWithFrame:CGRectMake(44, 0, kScreenWidth-44-51, 30) backgroundcolor:zrgbwhiteColor superView:self.navView];
    viewSeach.frameBY=7;
    viewSeach.layer.borderColor=rgb(229,229,229).CGColor;
    viewSeach.layer.borderWidth=1;
    viewSeach.layer.cornerRadius=3;
    UIImageView*imgVicon=[RHMethods imageviewWithFrame:CGRectMake(10, 8, 16, 16) defaultimage:@"xkf_search" supView:viewSeach];
    
    UITextField*tfSearch=[RHMethods textFieldlWithFrame:CGRectMake(imgVicon.frameXW+8, 0, viewSeach.frameWidth-imgVicon.frameXW-8-8, viewSeach.frameHeight) font:Font(10) color:nil placeholder:@"请输入公司名称等关键字" text:@""];
    [viewSeach addSubview:tfSearch];
    _searchTf=tfSearch;
    tfSearch.delegate=self;
     WSSizeButton*btnMe=[RHMethods buttonWithframe:CGRectMake(kScreenWidth-51, 0, 51, 30) backgroundColor:nil text:@"搜索" font:15 textColor:rgb(239,186,57) radius:0 superview:self.navView];
    [btnMe addTarget:self action:@selector(bigenSearch) forControlEvents:UIControlEventTouchUpInside];
    btnMe.frameRX=0;
    btnMe.centerY=viewSeach.centerY;
}

-(void)addAndRefreshView{
      __weak __typeof(self) weakSelf = self;
    self.view.backgroundColor=zrgbwhiteColor;
    if (scrollView==nil) {
        scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, kTopHeight, kScreenWidth, kScreenHeight-kTopHeight)];
        [self.view addSubview:scrollView];
        scrollView.delegate=self;
    }
    
    {
        UIView*viewSearhHistoryBig=_historyView;
        if (viewSearhHistoryBig==nil) {
            viewSearhHistoryBig=[RHMethods viewWithFrame:CGRectMake(0, 0, kScreenWidth, 0) backgroundcolor:zrgbwhiteColor superView:scrollView];
            _historyView=viewSearhHistoryBig;
            
            [RHMethods lableX:10 Y:15 W:0 Height:12 font:12 superview:viewSearhHistoryBig withColor:nil text:@"历史搜索"];
            WSSizeButton*btn1=[RHMethods buttonWithframe:CGRectMake(0, 0, 35, 35) backgroundColor:nil text:@"" font:0 textColor:nil radius:0 superview:viewSearhHistoryBig];
            [btn1 setImage:[UIImage imageNamed:@"xkf_delicon"] forState:UIControlStateNormal];
            [btn1 setBtnImageViewFrame:CGRectMake(10, 10, 15, 16)];
            btn1.frameRX=0;
            [btn1 addTarget:self action:@selector(deletBtnclick:) forControlEvents:UIControlEventTouchUpInside];
            
        }
        
        
        
        {
            UIView*viewHistory=[viewSearhHistoryBig getAddValueForKey:@"viewHistory"];
            
            if (viewHistory==nil) {
                viewHistory=[RHMethods viewWithFrame:CGRectMake(0, 35, kScreenWidth, 0) backgroundcolor:zrgbwhiteColor superView:viewSearhHistoryBig];
                [viewSearhHistoryBig setAddValue:viewHistory forKey:@"viewHistory"];
            }else{
            
                viewHistory.frameHeight=0;
                viewSearhHistoryBig.frameHeight=0;
                for (UIView*btn in viewHistory.subviews) {
                    btn.hidden=YES;
                }
            }
            
            
            NSArray*arraytitle=[_dateDic objectForKey:@"mysearch"];
            
            float mx=0;
            float my=0;
            for (int i=0; i<arraytitle.count; i++) {
                NSString*titleStr=[arraytitle[i] objectForKey:@"name"];
                
                WSSizeButton*btnHistory=[viewHistory getAddValueForKey:[NSString stringWithFormat:@"btnHistory%d",i]];
                if (btnHistory==nil) {
                    btnHistory=[RHMethods buttonWithframe:CGRectMake(0, 0, 0, 28) backgroundColor:zrgbwhiteColor text:titleStr font:12 textColor:rgb(51,51,51) radius:4 superview:viewHistory];
                    btnHistory.layer.borderWidth=1;
                    btnHistory.layer.borderColor=rgb(235,237,246).CGColor;
                    [viewHistory setAddValue:btnHistory forKey:[NSString stringWithFormat:@"btnHistory%d",i]];
                    
                }
                btnHistory.hidden=NO;
                btnHistory.frameX=mx+10;
                btnHistory.frameY=my;
                [btnHistory setTitle:titleStr forState:UIControlStateNormal];
                btnHistory.frameWidth=[titleStr widthWithFont:12]+32;
                if (btnHistory.frameXW>kScreenWidth-10) {
                    btnHistory.frameX=10;
                    
                    btnHistory.frameY=btnHistory.frameY+38;
                }
                
                mx=btnHistory.frameXW;
                my=btnHistory.frameY;
                viewHistory.frameHeight=btnHistory.frameYH+15;
                viewSearhHistoryBig.frameHeight=viewHistory.frameYH;
                [btnHistory addTarget:self action:@selector(itemBtnClick:) forControlEvents:UIControlEventTouchUpInside];
                
                
            }
            
            
        }
        scrollView.contentHeight=viewSearhHistoryBig.frameYH;
        
    }
    {
        UIView*viewSearhHistoryBig=_discoverView;
        if (viewSearhHistoryBig==nil) {
            viewSearhHistoryBig=[RHMethods viewWithFrame:CGRectMake(0, 0, kScreenWidth, 0) backgroundcolor:zrgbwhiteColor superView:scrollView];
            _discoverView=viewSearhHistoryBig;
            
            [RHMethods lableX:10 Y:15 W:0 Height:12 font:12 superview:viewSearhHistoryBig withColor:nil text:@"搜索发现"];
            WSSizeButton*btn1=[RHMethods buttonWithframe:CGRectMake(0, 0, 35, 35) backgroundColor:nil text:@"" font:0 textColor:nil radius:0 superview:viewSearhHistoryBig];
            [btn1 setImage:[UIImage imageNamed:@"xkf_llicon"] forState:UIControlStateNormal];
            [btn1 setImage:[UIImage imageNamed:@"xkf_lliconno"] forState:UIControlStateSelected];
            [btn1 setBtnImageViewFrame:CGRectMake(10-1.5, 10+1.5, 18, 12)];
            btn1.frameRX=0;
            
            [btn1 addViewClickBlock:^(UIView *view) {
                UIButton*mbtn=(UIButton*)view;
                mbtn.selected=!mbtn.selected;
                weakSelf.discoverBtnView.hidden=mbtn.selected;
                _showmesglb.hidden=!mbtn.selected;
                
                
            }];
        }
        viewSearhHistoryBig.frameY=scrollView.contentHeight;
        
        
        {
            UIView*viewHistory=[viewSearhHistoryBig getAddValueForKey:@"viewHistory"];
            
            
            if (viewHistory==nil) {
                viewHistory=[RHMethods viewWithFrame:CGRectMake(0, 35, kScreenWidth, 0) backgroundcolor:zrgbwhiteColor superView:viewSearhHistoryBig];
                [viewSearhHistoryBig setAddValue:viewHistory forKey:@"viewHistory"];
                _discoverBtnView=viewHistory;
                UILabel*lbyc=[RHMethods ClableY:45 W:0 Height:10 font:10 superview:viewSearhHistoryBig withColor:rgb(153,153,153) text:@"当前搜索发现已隐藏"];
                lbyc.hidden=YES;
                viewSearhHistoryBig.frameHeight=lbyc.frameYH+10;
                _showmesglb=lbyc;
            }else{
                
                viewHistory.frameHeight=0;
                viewSearhHistoryBig.frameHeight=0;
                for (UIView*btn in viewHistory.subviews) {
                    btn.hidden=YES;
                }
            }
            
            
            
            NSArray*arraytitle=[_dateDic objectForKey:@"search"];
            
            float mx=0;
            float my=0;
            for (int i=0; i<arraytitle.count; i++) {
                NSString*titleStr=[arraytitle[i] objectForKey:@"name"];
                
                WSSizeButton*btnHistory=[viewHistory getAddValueForKey:[NSString stringWithFormat:@"btnHistory%d",i]];
                if (btnHistory==nil) {
                    btnHistory=[RHMethods buttonWithframe:CGRectMake(0, 0, 0, 28) backgroundColor:zrgbwhiteColor text:titleStr font:12 textColor:rgb(51,51,51) radius:4 superview:viewHistory];
                    btnHistory.layer.borderWidth=1;
                    btnHistory.layer.borderColor=rgb(235,237,246).CGColor;
                    [viewHistory setAddValue:btnHistory forKey:[NSString stringWithFormat:@"btnHistory%d",i]];
                    
                    
                
                    
                }
                btnHistory.hidden=NO;
                btnHistory.frameX=mx+10;
                btnHistory.frameY=my;
                [btnHistory setTitle:titleStr forState:UIControlStateNormal];
                btnHistory.frameWidth=[titleStr widthWithFont:12]+32;
                if (btnHistory.frameXW>kScreenWidth-10) {
                    btnHistory.frameX=10;
                    
                    btnHistory.frameY=btnHistory.frameY+38;
                }
                
                mx=btnHistory.frameXW;
                my=btnHistory.frameY;
                viewHistory.frameHeight=btnHistory.frameYH+15;
                viewSearhHistoryBig.frameHeight=viewHistory.frameYH;
                [btnHistory addTarget:self action:@selector(itemBtnClick:) forControlEvents:UIControlEventTouchUpInside];
                
                
            }
            
            
        }
        
        scrollView.contentHeight=viewSearhHistoryBig.frameYH;
        kScrollViewCouldScroll(scrollView);
    }
    
    
    
}
#pragma mark - 请求数据
-(void)loadDATA{
    krequestParam
    
    [NetEngine createPostAction:@"products/search" withParams:dictparam onCompletion:^(id resData, BOOL isCache) {
        if ([[resData getSafeObjWithkey:@"status"] isEqualToString:@"200"]) {
            NSDictionary *dic=[resData getSafeObjWithkey:@"data"];
            _dateDic=dic;
            [self addAndRefreshView];
            if (_successStr) {
                [SVProgressHUD showSuccessWithStatus:_successStr];
                _successStr=nil;
            }
            
//            [SVProgressHUD showSuccessWithStatus:@"<#成功#>"];
            //            [self addView];
            
        }else{
            [SVProgressHUD showErrorWithStatus:[resData valueForJSONKey:@"info"]];
            
        }
    }];
    
}
#pragma mark - 事件监听
-(void)deletBtnclick:(UIButton*)btn{
    
    [MyUiIAlertView showAlerttWithTitle:@"您确定删除搜索记录吗？" message:nil cancelButtonTitle:@"取消" otherButtonTitles:@"确定" WithAlertBlock:^(UIAlertView * _Nonnull alertView, NSInteger buttonIndex) {
        if (buttonIndex==1) {
            krequestParam
            
            [NetEngine createPostAction:@"products/delsearch" withParams:dictparam onCompletion:^(id resData, BOOL isCache) {
                if ([[resData getSafeObjWithkey:@"status"] isEqualToString:@"200"]) {
                    //            NSDictionary *dic=[resData getSafeObjWithkey:@"data"];
                    [self loadDATA];
                    _successStr=[resData valueForJSONKey:@"info"];
                }else{
                    [SVProgressHUD showErrorWithStatus:[resData valueForJSONKey:@"info"]];
                    
                }
            }];
        }
    }];
    
    
  
    
    
}
-(void)itemBtnClick:(UIButton*)btn{
    
    _searchTf.text=btn.currentTitle;
    [self bigenSearch];
    
    
}

-(void)bigenSearch{
    [self.view endEditing:YES];
    if (![_searchTf.text notEmptyOrNull]) {
        [SVProgressHUD showImage:nil status:@"请输入搜索字段"];
        return;
    }

    
    [self pushController:[V4LicensePlateViewController class] withInfo:@"search" withTitle:_searchTf.text withOther:@{@"searchkey":_searchTf.text}];
    
    
    
}
#pragma mark - 代理事件
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
//    [self.view endEditing:YES];
    [self bigenSearch];
    return YES;
}
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    [self.view endEditing:YES];

}
@end
